package com.jojo.jojoWorld.controller;

import com.alibaba.excel.EasyExcel;
import com.easyExcel.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * EasyExcelController
 *
 * @author byc
 * @date 2022-03-19
 */
@RestController
@RequestMapping("/api/excel")
public class EasyExcelController {

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    /***
     * 文件导入
     *
     * @return List<com.easyExcel.entity.User>
     */
    @GetMapping("/read")
    public List<User> read() throws FileNotFoundException {
        File file = new File("C:\\Users\\Lenovo\\Desktop\\人员表.xlsx");
        InputStream inputStream = new FileInputStream(file);
        List<User> list = EasyExcel.read(inputStream)
                .head(User.class)
                .sheet()
                .headRowNumber(1)
                .doReadSync();
        return list;
    }

    /**
     * 导出
     * @return
     */
    @GetMapping("outPut")
    public String outPut(){
        List<User> list = new ArrayList<>();
        User user1 = new User();
        user1.setId(1);
        user1.setName("卢本伟");
        user1.setAge(12);
        list.add(user1);
        String pathName = "C:\\Users\\Lenovo\\Desktop\\人员表导出.xlsx";

        EasyExcel.write(pathName,User.class).sheet(1).doWrite(list);
        return "success";
    }

}
